package org.apix.configuration;

import java.io.*;

import org.bukkit.Location;
import org.bukkit.entity.Player;
import org.bukkit.util.config.Configuration;

public class PlayerStorageData {
	
	protected Configuration conf;
	protected Player player;
	
	public PlayerStorageData(Player player) {
		this.player = player;
		Location loc = player.getLocation();
		String world = loc.getWorld().getName();
		if(!new File("plugins/Apix").exists()){
        	new File("plugins/Apix").mkdir();
        	new File("plugins/Apix/"+world).mkdir();
        }		
		File f = new File("plugins/Apix/"+world, player.getName() + ".yml");
		conf = new Configuration(f);
		if (f.exists())
        {        	
        	conf.load();
        }
        else {
            conf.save();
        }       
	}
	
	public static PlayerStorageData getInstance(Player player) {
		  return new PlayerStorageData(player);
	}

	public void setCooldown(String cooldown) {
		conf.setProperty("cooldown."+cooldown, new Long(System.currentTimeMillis()));
		conf.save();
	}
	
	public long getCooldown(String cooldown) {
		return Long.getLong(conf.getString(cooldown,"0"));		
	}
	
	
	
	public Location setWarp(String warpName) {
		Location loc = this.player.getLocation();
		
		conf.setProperty("warp." + warpName + ".x", loc.getX());
		conf.setProperty("warp." + warpName + ".y", loc.getY());
		conf.setProperty("warp." + warpName + ".z", loc.getZ());
		conf.setProperty("warp." + warpName + ".yaw", loc.getYaw());
		conf.setProperty("warp." + warpName + ".pitch", loc.getPitch());
		conf.save();		
		
		
		return loc;
	}
	
	public Location setHome() {		
		return setWarp("home");
	}
	
	public Location getWarp(String warpName) {
		conf.load();
		Location loc = player.getLocation();
	
		return new Location(player.getWorld(), conf.getDouble("warp." + warpName + ".x", loc.getX()), 
				conf.getDouble("warp." + warpName + ".y", loc.getY()), conf.getDouble("warp." + warpName + ".z", loc.getZ()), (float)conf.getDouble("warp." + warpName + ".yaw", loc.getYaw()), (float)conf.getDouble("warp." + warpName + ".pitch", loc.getPitch()));
	}
	
	public Location getHome() {
		return getWarp("home");
	}
}
